var map;
var oldmark;
var gc;
function initMap(container) {
	map = new BMap.Map(container); // 创建Map实例
	map.enableScrollWheelZoom(); // 启用滚轮放大缩小。
	map.enableKeyboard(); // 启用键盘操作。
	map.addControl(new BMap.NavigationControl()); // 增加鱼骨控件
	gc = new BMap.Geocoder();
}
function recenterMap(center) {
	map.centerAndZoom(center, 15); // 初始化地图，设置中心点坐标和地图级别
}
function createDraggableMarker(point, gragendFunc) {
	var marker = new BMap.Marker(point); // 创建标注
	marker.enableDragging(true); // 设置标注可拖拽
	marker.addEventListener('dragend',function(e) {
		gragendFunc(e);
	});
	return marker;
}
function processAddress(point,cityId,detailAddressId) {
	try {
		gc.getLocation(point, function(rs){
			var addComp = rs.addressComponents;
			if($.isFunction(getCountries)) {
				if(addComp.city != '') {
					if($("#"+cityId+" option:selected").html() != addComp.city) {
						getCountries('中国',addComp.province,addComp.city);
					}
				} else {
					getCountries('','','');
				}
			}
			$("#"+detailAddressId).val(rs.address);
		});
	}catch(e){}
}
function popAddressInfo(marker,point,nameId) {
	try {
		gc.getLocation(point, function(rs){
			var name = $('#'+nameId).val();
			if(!name) {
				name = '';
			}
			marker.openInfoWindow(new BMap.InfoWindow('<div style="font-size:14px;font-weight:bold;color:#D14836">'+name+'</div><div style="font-size:12px">地址：'+rs.address+'</div>'));
		});
	}catch(e){}
}
function addMarkerOnMap(map, marker) {
	map.addOverlay(marker); // 将标注添加到地图中
}
function createAutocomplete(suggestId, city, successBack) {
	var ac = new BMap.Autocomplete({input:suggestId,location:city});
	ac.addEventListener('onconfirm',function(e){
		var location = e.item.value;
		successBack(location);
	});
	return ac;
}
function serchFromCityByName(city, name, resultPanel, successBack) {
	if (name != '' && city != '') {
		var opts = {
			onSearchComplete : function(result) {
				switch (local.getStatus()) {
					case BMAP_STATUS_SUCCESS: // 数据返回成功
						var po = result.getPoi(0).point; // 获取第一个点，并设置为中心
						map.centerAndZoom(po, 15);
						successBack(po);
						break;
					case BMAP_STATUS_UNKNOWN_LOCATION: // 数据无返回
						alert("对不起无法找到您要找的地址");
						local.search(city,{forceLocal:true}); // 如果在没搜到，则返回该市
						break;
					default:
						break;
				}
			},
			renderOptions : {
				map : map,
				panel : resultPanel
			}	
		};
		var local = new BMap.LocalSearch(city, opts);
		local.search(name,{forceLocal:true});
	}
}
function addInfoWindowToMarker(marker, infoWindow) {
	marker.addEventListener("click", function() {
		this.openInfoWindow(infoWindow);
		// 图片加载完毕重绘infowindow,infoWindow中必须得有图片
		document.getElementById('imgId').onload = function() {
			infoWindow.redraw();
		}
	});
}
function addPolylineToMap(map, polylineArr) {
	var polyline = new BMap.Polyline(polylineArr, {
		strokeColor : "blue",
		strokeWeight : 3,
		strokeOpacity : 0.5
	});
	map.addOverlay(polyline);
}
//
// 添加线路
//
function addTravelPath(map, spotObjectArr) {
	var i = 0;
	var pointArr = new Arrary();
	for (i = 0; i < spotObjectArr.length; i++) {
		var marker = new BMap.Marker(spotObjectArr[i].point);
		pointArr.push(spotObjectArr[i].point);
		addIntoWindowToMarker(marker, spotObjectArr[i].infoWindow);
		addMarkerToMap(map, marker);
	}
	addPolylineToMap(map, pointArr);
}